﻿Imports Mysoft.Map.Data
Partial Public Class SelectDepartmentMulti
    Inherits Mysoft.Map.Application.Security.AppPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Request.QueryString("DeptList") Is Nothing Then
            'txtDeptList.Text = Request.QueryString("DeptList")
        End If
        If Not IsPostBack Then
            repCompany.BindMode = 1
            repCompany.DataTable = GetBUName()
            repCompany.DataBind()
        End If
    End Sub

    '获取部门信息数据
    Private Function GetBUName() As DataTable
        Dim SQL As String
        Dim DT As DataTable
        Dim strHierarchyCode As String

        'SQL = " SELECT OrderHierarchyCode AS code, BUName AS name, BUGUID AS guid, '0' AS type, '0' AS checked" & _
        '      " FROM mybusinessunit" & _
        '      " WHERE ((CompanyGUID='" + Session("BUGUID") + "' AND BUType=1) or (parentGUID='11B11DB4-E907-4F1F-8835-B9DAAB6E1F23' and BUType=1 )) " & _
        '      " ORDER BY OrderHierarchyCode"

        'SQL = " SELECT bu.hierarchyCode AS code,bu.Level,bu.BUGUID AS guid,bu.BUName,bu.companyguid,bu.IsEndCompany,bu.butype,bu.hierarchyCode,bu.ProjGUID,bu.ParentGUID,bu.BuName as name,'0' AS checked FROM  " & _
        '      " myBusinessUnit bu WHERE (bu.BUType IN (0,1)) and (companyguid = '" + Session("BUGUID") + "'  or parentguid is null) or (parentguid = '11B11DB4-E907-4F1F-8835-B9DAAB6E1F23' and BUType = 1) " & _
        '      " ORDER BY bu.OrderHierarchyCode "
        If MyDB.GetDataItemString("SELECT Count(8) FROM myBusinessUnit WHERE Level=0 AND IsCompany=1 AND BUGUID='" & Session("BUGUID") & "'") <> "0" Then
            SQL = " SELECT bu.hierarchyCode AS code,bu.Level,bu.BUGUID AS guid,bu.BUName,bu.companyguid,bu.IsEndCompany,bu.butype,bu.hierarchyCode,bu.ProjGUID,bu.ParentGUID,bu.BuName as name,'0' AS checked,OrderHierarchyCode,0 AS ordercode FROM " & _
              " myBusinessUnit bu WHERE (parentguid is NULL) OR (parentguid = '11B11DB4-E907-4F1F-8835-B9DAAB6E1F23' and BUType = 1) " & _
              " ORDER BY ordercode,OrderHierarchyCode "
        Else
            SQL = " SELECT bu.hierarchyCode AS code,bu.Level,bu.BUGUID AS guid,bu.BUName,bu.companyguid,bu.IsEndCompany,bu.butype,bu.hierarchyCode,bu.ProjGUID,bu.ParentGUID,bu.BuName as name,'0' AS checked,OrderHierarchyCode,0 AS ordercode FROM " & _
              " myBusinessUnit bu WHERE (parentguid is NULL) OR (parentguid = '11B11DB4-E907-4F1F-8835-B9DAAB6E1F23' and BUType = 1) " & _
              " UNION ALL " & _
              " SELECT bu.hierarchyCode AS code,bu.Level,bu.BUGUID AS guid,bu.BUName,bu.companyguid,bu.IsEndCompany,bu.butype,bu.hierarchyCode,bu.ProjGUID,bu.ParentGUID,bu.BuName as name,'0' AS checked,OrderHierarchyCode,1 AS ordercode FROM " & _
              " myBusinessUnit bu WHERE (bu.BUType IN (0,1)) and (charindex(HierarchyCode+'.',(SELECT HierarchyCode FROM myBusinessUnit WHERE BUGUID = '" + Session("BUGUID") + "')) > 0 OR companyguid = '" + Session("BUGUID") + "') AND parentguid IS NOT NULL " & _
              " ORDER BY ordercode,OrderHierarchyCode "
        End If

        DT = MyDB.GetDataTable(SQL)

        GetBUName = DT

    End Function

End Class